package com.dangdang.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

import com.dangdang.domain.ReceiveAddress;

/**
 * 用给定的对象类型和request构建对象
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-16
 */
public class FormUtils {

	/**
	 * 将request 里面的东西 填充到这里来
	 * 
	 */
	public static void populate(Object obj, HttpServletRequest request) {
		try {
			BeanUtils.populate(obj, request.getParameterMap());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 将地址类封装成对象
	 * 
	 * @param receiveAddress
	 * @param request
	 * @return
	 */
	public static ReceiveAddress populate(ReceiveAddress receiveAddress,
			HttpServletRequest request) {
		System.out.println("实验到此");

		/** 收件人姓名 */
		String receiveName = request.getParameter("realName");

		/** 收件人详细地址 */
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String address = request.getParameter("address");
		String detailAddress = province + city + address;
		/** 邮政编码 */
		String postalCode = request.getParameter("postalCode");

		/** 手机号码 */
		String phone = request.getParameter("phone");

		receiveAddress.setReceiveName(receiveName);
		receiveAddress.setDetailAddress(detailAddress);
		receiveAddress.setPostalCode(postalCode);
		receiveAddress.setPhone(phone);
		return receiveAddress;

	}
}
